using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class UserLotteryCardDal
	{
		public bool Exists(string CardNumber)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from MBF_UserLotteryCard");
			stringBuilder.Append(" where ");
			stringBuilder.Append("CardNumber = @CardNumber ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardNumber", SqlDbType.NVarChar, 50)
			};
			array[0].Value = CardNumber;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public void Add(UserLotteryCardModel model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into MBF_UserLotteryCard(");
			stringBuilder.Append("CardNumber,FaceValue,Balance,EffectiveTime,UserID,IsDeleted,remark");
			stringBuilder.Append(") values (");
			stringBuilder.Append("@CardNumber,@FaceValue,@Balance,@EffectiveTime,@UserID,@IsDeleted,@remark");
			stringBuilder.Append(") ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@FaceValue", SqlDbType.Decimal, 9),
				new SqlParameter("@Balance", SqlDbType.Decimal, 9),
				new SqlParameter("@EffectiveTime", SqlDbType.DateTime),
				new SqlParameter("@UserID", SqlDbType.Int, 4),
				new SqlParameter("@IsDeleted", SqlDbType.Bit, 1),
				new SqlParameter("@remark", SqlDbType.Text)
			};
			array[0].Value = model.CardNumber;
			array[1].Value = model.FaceValue;
			array[2].Value = model.Balance;
			array[3].Value = model.EffectiveTime;
			array[4].Value = model.UserID;
			array[5].Value = model.IsDeleted;
			array[6].Value = model.remark;
			Conn.ExecuteSql(stringBuilder.ToString(), array);
		}
		public bool Update(UserLotteryCardModel model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update MBF_UserLotteryCard set ");
			stringBuilder.Append(" CardNumber = @CardNumber , ");
			stringBuilder.Append(" FaceValue = @FaceValue , ");
			stringBuilder.Append(" Balance = @Balance , ");
			stringBuilder.Append(" EffectiveTime = @EffectiveTime , ");
			stringBuilder.Append(" UserID = @UserID , ");
			stringBuilder.Append(" IsDeleted = @IsDeleted , ");
			stringBuilder.Append(" remark = @remark  ");
			stringBuilder.Append(" where CardNumber=@CardNumber  ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@FaceValue", SqlDbType.Decimal, 9),
				new SqlParameter("@Balance", SqlDbType.Decimal, 9),
				new SqlParameter("@EffectiveTime", SqlDbType.DateTime),
				new SqlParameter("@UserID", SqlDbType.Int, 4),
				new SqlParameter("@IsDeleted", SqlDbType.Bit, 1),
				new SqlParameter("@remark", SqlDbType.Text)
			};
			array[0].Value = model.CardNumber;
			array[1].Value = model.FaceValue;
			array[2].Value = model.Balance;
			array[3].Value = model.EffectiveTime;
			array[4].Value = model.UserID;
			array[5].Value = model.IsDeleted;
			array[6].Value = model.remark;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(string CardNumber)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from MBF_UserLotteryCard ");
			stringBuilder.Append(" where CardNumber=@CardNumber ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardNumber", SqlDbType.NVarChar, 50)
			};
			array[0].Value = CardNumber;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public UserLotteryCardModel GetModel(string CardNumber)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select CardNumber, FaceValue, Balance, EffectiveTime, UserID, IsDeleted, remark  ");
			stringBuilder.Append("  from MBF_UserLotteryCard ");
			stringBuilder.Append(" where CardNumber=@CardNumber ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardNumber", SqlDbType.NVarChar, 50)
			};
			array[0].Value = CardNumber;
			UserLotteryCardModel userLotteryCardModel = new UserLotteryCardModel();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			UserLotteryCardModel result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				userLotteryCardModel.CardNumber = dataSet.Tables[0].Rows[0]["CardNumber"].ToString();
				if (dataSet.Tables[0].Rows[0]["FaceValue"] != null && dataSet.Tables[0].Rows[0]["FaceValue"].ToString() != "")
				{
					userLotteryCardModel.FaceValue = new decimal?(decimal.Parse(dataSet.Tables[0].Rows[0]["FaceValue"].ToString()));
				}
				if (dataSet.Tables[0].Rows[0]["Balance"] != null && dataSet.Tables[0].Rows[0]["Balance"].ToString() != "")
				{
					userLotteryCardModel.Balance = new decimal?(decimal.Parse(dataSet.Tables[0].Rows[0]["Balance"].ToString()));
				}
				if (dataSet.Tables[0].Rows[0]["EffectiveTime"] != null && dataSet.Tables[0].Rows[0]["EffectiveTime"].ToString() != "")
				{
					userLotteryCardModel.EffectiveTime = new System.DateTime?(System.DateTime.Parse(dataSet.Tables[0].Rows[0]["EffectiveTime"].ToString()));
				}
				if (dataSet.Tables[0].Rows[0]["UserID"] != null && dataSet.Tables[0].Rows[0]["UserID"].ToString() != "")
				{
					userLotteryCardModel.UserID = new int?(int.Parse(dataSet.Tables[0].Rows[0]["UserID"].ToString()));
				}
				if (dataSet.Tables[0].Rows[0]["IsDeleted"] != null && dataSet.Tables[0].Rows[0]["IsDeleted"].ToString() != "")
				{
					if (dataSet.Tables[0].Rows[0]["IsDeleted"].ToString() == "1" || dataSet.Tables[0].Rows[0]["IsDeleted"].ToString().ToLower() == "true")
					{
						userLotteryCardModel.IsDeleted = new bool?(true);
					}
					else
					{
						userLotteryCardModel.IsDeleted = new bool?(false);
					}
				}
				userLotteryCardModel.remark = dataSet.Tables[0].Rows[0]["remark"].ToString();
				result = userLotteryCardModel;
			}
			else
			{
				result = null;
			}
			return result;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * ");
			stringBuilder.Append(" FROM MBF_UserLotteryCard ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string fieldOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * ");
			stringBuilder.Append(" FROM MBF_UserLotteryCard ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + fieldOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetListRowid(int Top, string strWhere, string fieldOrder, params string[] selectField)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			string text = "*";
			if (selectField.Length > 0)
			{
				text = selectField[0];
				if (text.Trim() == "")
				{
					text = "*";
				}
			}
			stringBuilder.AppendFormat(" ROW_NUMBER() OVER (ORDER BY CardNumber ASC) rowid,{0} ", text);
			stringBuilder.Append(" FROM MBF_UserLotteryCard ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + fieldOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
